/**
 * 
 */
package front2;

import java.util.ArrayList;
import java.util.Scanner;
import front.*;

/**
 * @author Yuuki
 * Have the same function as the subforum jsp.
 *
 */
public class Root extends Commands implements frontRunnable {
	
	public Root(Scanner s){
		Commands.s =s;
	}
	
	public int run(){
		while(true){
			print();
			if(readCommand())
				continue;
			
			if(isDown()){
				int number = Integer.parseInt(ret[1]);
				//enter a subForum
				Sub s = new Sub(number);
				if(s.run() == -1)
					return -1;
			}
			
			if(userID >= 0){
				if(userID == 2){
					if(isAdd()){
						String subForumName = readCommand("SubForum Name?");
						int lvl = Integer.parseInt(readCommand("MinimumRestrictionLevel? -tip: it should be 0"));
						addSubForum(subForumName, lvl);
					}
				}
				else{
					System.out.println("Access Denied!");
				}
			}else{
				System.out.println("This feature is not available for guests! please register and login!");
			}
			
			if(isLogout())
				return -1;
			if(isUp())
				return 0;
		}
	}
	public void print(){
		ArrayList<SubForum> subs = m.getAllSubForums();
		
		for(SubForum s : subs)
			System.out.println(s.getSubForumID() +"  "+ s.getName() + " " + s.getNumberOfThreads());
	}
	
	public void addSubForum(String name, int lvl){
		
		if(name.equals("") || name.isEmpty()){
			System.out.println("Add Error! Usage: add <nameOfSubForum> <minimumRestrictionLevel>");
		}
		else{
			front.SubForum s= new SubForum(0, name, lvl);
			s.Create();
		}
	}
}
